package rs.mbs2.epics.server.servlet;

import java.io.IOException;
import java.util.Date;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import rs.mbs2.epics.server.entity.Album;
import rs.mbs2.epics.server.entity.User;
import rs.mbs2.epics.server.session.dao.localinterface.AlbumDaoLocal;

@SuppressWarnings("serial")
public class ChangeFavoritesVisibilityController extends HttpServlet {
	
	private static Logger log = Logger.getLogger(ChangeFavoritesVisibilityController.class);
	
	@EJB
	AlbumDaoLocal albumDao;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int albumId = Integer.parseInt(request.getParameter("albumId"));
		boolean visible = Boolean.parseBoolean(request.getParameter("visible"));
		
		Album album = albumDao.findById(albumId);
		album.setPubliclyAvailable(!visible);
		albumDao.merge(album);
		
		User user = (User)request.getSession().getAttribute("user");
		log.info(new Date() + " Korisnik[id]:" + user.getId() + " izmenio vidljivost albuma omiljenih slika na: " + !visible);
		
		request.getRequestDispatcher("/ViewAlbumController?id=" + albumId).forward(request, response);		
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
